<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/5/20 0020
 * Time: 上午 11:34
 */
namespace app\model;
class UserResume extends \think\Model{
    public $table="user_resume";

    /**
     * 根据用户ID，返回他的头像名字
     * @param $user_id
     * @return mixed
     */
    function get_head_img_name_by_user_id($user_id){
        $o=$this->where(['user_id'=>$user_id])->field("head_img,name")->find();
        $arr=object2array($o);
        $arr['head_img']=pic_host($arr['head_img']);
        if($arr['name']==false){
            $arr['name']="无";
        }
        return $arr;
    }

    /**
     * 根据用户ID,返回他简历中的基本信息
     * @param $user_id
     * @return mixed
     */
    function get_info($user_id){
        $o=$this->where(['user_id'=>$user_id])->find();
        $arr=object2array($o);
        $this->toTypeToName($arr);
        return $arr;

    }

    function toTypeToName(&$arr){
        if($arr['head_img']==false){
            $arr['head_img']=pic_host($arr['head_img']);
        }
        if(!if_null($arr['sex_type'])){
            $arr['sex_type_name']=config("web.sex_type")[$arr['sex_type']];
        }
        if(!if_null($arr['edu_type'])){
            $arr['edu_type_name']=config('web.edu_type')[$arr['edu_type']];
        }
        if(!if_null($arr['pic'])){
            $arr['pic_arr']=explode(';',$arr['pic']);
        }
        if($arr['job_in']!=false){
            $d=diffDate(date('Y-m-d'),$arr['job_in']);
            $arr['job_in_year']=$d['year'];
        }
        if($arr['birth_day']!=false){
            $d=diffDate(date('Y-m-d'),$arr['birth_day']);
            $arr['age']=$d['year'];
        }
        $Places=new Places();
        if($arr['live_province_id']!=false){
            $arr['province_name']=$Places->get_name_by_id($arr['live_province_id']);
        }
        if($arr['live_area_id']!=false){
            $arr['city_name']=$Places->get_name_by_id($arr['live_area_id']);
        }
        return $arr;
    }

    /**
     * 保存用户简历基本信息
     * @param $data
     * @return false|int|mixed|string
     */
    function save_user_resume($data){
        $id=$this->where(['user_id'=>$data['user_id']])->value("id");


        $tem_data['head_img']=$data['head_img'];
        $tem_data['name']=$data['name'];
        $tem_data['sex_type']=$data['sex_type'];
        $tem_data['birth_day']=$data['birth_day'];
        $tem_data['edu_type']=$data['edu_type'];
        $tem_data['job_in']=$data['job_in'];
        $tem_data['live_area_id']=$data['live_area_id'];
        $tem_data['mobile']=$data['mobile'];

        $tem_data['live_province_id']=$data['live_province_id'];

        if($id==false){
            $tem_data['created_at']=cur_time();
            $tem_data['user_id']=$data['user_id'];
            $id=$this->insert($tem_data,false,true);
            return $id;
        }else{
            $tem_data['updated_at']=cur_time();
            return $this->where(['user_id'=>$data['user_id']])->update($tem_data);
        }


    }

    /**
     * 保存简历中的自我评论
     * @param $data
     * @return $this|int|string
     */
    function save_evaluate($data){
        $tem_data['evaluate']=$data['evaluate'];
        $id=$this->where(['user_id'=>$data['user_id']])->value("id");
        if($id){
            $tem_data['updated_at']=cur_time();
            return $this->where(['user_id'=>$data['user_id']])->update($tem_data);
        }else{
            $tem_data['user_id']=$data['user_id'];
            return $this->insert($tem_data,false,true);
        }
    }

    /**
     * 保存简历中的生活照图片列表
     * @param $data
     * @return $this|int|string
     */
    function save_pic($data){
        $id=$this->where(['user_id'=>$data['user_id']])->value("id");
        if($id){
            $tem_data['pic']=$data['pic'];

            return $this->where(['user_id'=>$data['user_id']])->update($tem_data);
        }else{
            $tem_data['pic']=$data['pic'];
            $tem_data['user_id']=$data['user_id'];
            return $this->insert($tem_data,false,true);
        }
    }

    /**
     * 根据用户ID,返回他的简历中的主键ID
     * @param $user_id
     * @return string
     */
    function get_id_by_user_id($user_id){
        $o=$this->where(['user_id'=>$user_id])->column('id');
        if($o[0]){
            return $o[0];
        }else{
            return '';
        }
    }

    /**
     * 取得所有用户的简历
     */
    function get_user_resume($page=1,$params=[]){
        $offset=($page-1)*10;

        $arrs=$this->limit($offset,10)->select();
        $arrs=object2array($arrs);
        $User=new User();
        foreach($arrs as $k=>&$v){
            $this->toTypeToName($v);
            $arrs[$k]['exp']=$User->get_exp($v['user_id']);
        }

        return $arrs;
    }

    /**
     * 根据简历主键，返回他的对应的用户ID
     */
    function get_user_id_by_id($id){
        $o=$this->where(['id'=>$id])->column("user_id");
        return $o[0];
    }
}